﻿using BySoft.Server.DataAccess;
using OneSoft.DataAccess;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;


//==================================================================================
//
//        Copyright (C) 2014-2016 SRC Studio   
//        All rights reserved
//
//        Filename :BaseBusiness
//
//        Created by Administrator at  2016-05-20 22:15:44
//        http://www.seven-rosy-clouds.com
//
//        Description :  
//
//        Change History :
//
//	Version    Date                      Author           Description  
// ----------------------------------------------------------------------------------
//  	1.0  2016-05-20 22:15:44        Administrator    Created.
//   
//
//===================================================================================

namespace BySoft.Server.Business
{

    public class BusinessEntityBase<TEntity> where TEntity : EntityBase 
    {
        public static EntityProperty EntityTableProperty{       get;set;  }


       
        public  bool IsIDExists(string inputValue)
        {
            try
            {
                EntityProperty tbl = EntityTableProperty;

                return ClsComm.CheckExist(tbl.TableName, tbl.IDString, inputValue, true);
                //MdCustomerDA.IsCustomerNameExists(customerName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public  bool IsNameExists(string inputValue,string currentID)
        {
            try
            {
                EntityProperty tbl = EntityTableProperty;

                return ClsComm.CheckNameWithoutID(tbl.TableName,tbl.IDString,currentID, tbl.NameString, inputValue, true);
              
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool Save(TEntity entity)
        {
            bool isResult = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    DataAccessCommon.Insert(entity, broker);
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = false;
                    broker.Rollback();
                    throw ex;
                }

            }
            return isResult;
        }



        public static bool Update(TEntity entity)
        {
            bool isResult = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    DataAccessCommon.Update(entity, broker);
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = false;
                    broker.Rollback();
                    throw ex;
                }

            }
            return isResult;
        }

        public  void Del(string strCode)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();

                    EntityProperty prop = EntityTableProperty;

                   

                    ClsCommDA.Del(prop.TableName, prop.IDString, strCode, broker);
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        /// <summary>
        /// 检查数字不能为空或0
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
      public static bool IsEmptyOrZero(string str)
        {
            return (string.IsNullOrEmpty(str) || "0.00".Equals(str) || "0".Equals(str));

        }

     
        //public static DataTable SelectList(string strCustomerID, string strCustomerName, string UserID, int pageIndex, int pageSize, out int count)
        //{
        //    return MdCustomerDA.SelectList(strCustomerID, strCustomerName, UserID, pageIndex, pageSize, out count);
        //}

        //public static DataTable SelectDialogList(string strCustomerID, string strCustomerName, string UserID, int pageIndex, int pageSize, out int count)
        //{
        //    return MdCustomerDA.SelectDialogList(strCustomerID, strCustomerName, UserID, pageIndex, pageSize, out count);
        //}

        //public static DataTable SelectDialogListByCustomerContactRecord(string strCustomerID, string strCustomerName, string UserID, int pageIndex, int pageSize, out int m_count)
        //{
        //    return MdCustomerDA.SelectDialogListByCustomerContactRecord(strCustomerID, strCustomerName, UserID, pageIndex, pageSize, out m_count);
        //}


      public static DataTable CreateUpFileDT()
      {
          DataTable dt = new DataTable();
          dt.Columns.Add("AttachmentID");
          dt.Columns.Add("FileType");
          dt.Columns.Add("FileSize");
          dt.Columns.Add("UpDateTime");
          dt.Columns.Add("FileURL");
          dt.Columns.Add("FileName");
          dt.Columns.Add("FormType");
          dt.Columns.Add("FormID");
          dt.Columns.Add("Status");
          dt.Columns.Add("OpenURL");
          return dt;
      }

        /// <summary>
      /// 根据ID取得session里存放的DT，
      /// 产品里的技术文件存在Session["DtTechFile"]里
      /// 其它放在Session["DtMapFile"]里
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
      public static string QuerySessionFileName(string id)
      {
          string strSessionName="";
           if (id.Equals("6"))
          {
              strSessionName = "DtTechFile";
          }
           else
           {//if (id.Equals("5")) "7"

          
              strSessionName = "DtMapFile";
          }
         

         
          return strSessionName;
      }

     

    }

   public class EntityProperty
   {
       public string IDString { get; set; }
       public string NameString { get; set; }
       public string TableName { get; set; }
   }
}
